﻿using System;
using System.Data;
using System.Collections;
using CE.Database;

/// <summary>
/// Summary description for FleetCarDetailDB
/// </summary>
namespace CE.CarInsurance
{
    public class FleetCarDetailDB
    {
        public Int64 Insert(FleetCarDetail fleetcardetail)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "sprocFleetCarDetailInsertUpdateSingleItem";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@FleetCarId", fleetcardetail.FleetCarId);
            _sPara.Add("@CompanyId", fleetcardetail.CompanyId);
            _sPara.Add("@CarMake", fleetcardetail.CarMakeName);
            _sPara.Add("@CarModel", fleetcardetail.CarModelName);
            _sPara.Add("@Body", fleetcardetail.Body);
            _sPara.Add("@CC", fleetcardetail.CC);
            _sPara.Add("@Door", fleetcardetail.Door);
            _sPara.Add("@GroupNo", fleetcardetail.GroupNo);
            _sPara.Add("@MakeModelDesc", fleetcardetail.MakeModelDesc);
            _sPara.Add("@ManufactYear", fleetcardetail.ManufactYear);
            _sPara.Add("@CurrentStep", fleetcardetail.CurrentStep);
            _sPara.Add("@TotalStep", fleetcardetail.TotalStep);
            _sPara.Add("@AgentId", fleetcardetail.AgentId);
            _sPara.Add("@RefCEUserId", fleetcardetail.RefCEUserId);
            _sPara.Add("@Status", fleetcardetail.Status);
            _sPara.Add("@Info1", fleetcardetail.Info1);
            _sPara.Add("@Info2", fleetcardetail.Info2);
            _sPara.Add("@Info3", fleetcardetail.Info3);
            _sPara.Add("@Info4", fleetcardetail.Info4);
            _sPara.Add("@Info5", fleetcardetail.Info5);
            _sPara.Add("@Info6", fleetcardetail.Info6);
            _sPara.Add("@Info7", fleetcardetail.Info7);
            _sPara.Add("@Info8", fleetcardetail.Info8);
            _sPara.Add("@Info9", fleetcardetail.Info9);
            _sPara.Add("@Info10", fleetcardetail.Info10);
            _sPara.Add("@Info11", fleetcardetail.Info11);
            _sPara.Add("@Info12", fleetcardetail.Info12);
            _sPara.Add("@Info13", fleetcardetail.Info13);
            _sPara.Add("@Info14", fleetcardetail.Info14);
            _sPara.Add("@Info15", fleetcardetail.Info15);
            _sPara.Add("@Info16", fleetcardetail.Info16);
            _sPara.Add("@Info17", fleetcardetail.Info17);
            _sPara.Add("@Info18", fleetcardetail.Info18);
            _sPara.Add("@Info19", fleetcardetail.Info19);
            _sPara.Add("@Info20", fleetcardetail.Info20);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public bool UpdateCarOther(Int64 FleetCarId, bool IsCarOther)
        {
            MyDatabase _db = new MyDatabase();
            string strSQL = "UPDATE tblFleetCarDetail SET BInfo1 = @IsCarOther WHERE FleetCarId = @FleetCarId";
            int _result = 0;
            SortedList _sPara = new SortedList();

            _sPara.Add("@FleetCarId", FleetCarId);
            if (IsCarOther)
            {
                _sPara.Add("@IsCarOther", 1);
            }
            else
            {
                _sPara.Add("@IsCarOther", DBNull.Value);
            }

            _result = _db.ExecuteNonSqlStatement(strSQL, _sPara);
            return _result > 0;
        }

        public FleetCarDetail GetCar(Int64 FleetCarId, Int64 CompanyId)
        {
            FleetCarDetail _fleetcardetail = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocFleetCarDetailSelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@FleetCarId", FleetCarId);
            _spara.Add("@CompanyId", CompanyId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _fleetcardetail = new FleetCarDetail();
                    _fleetcardetail = FillDataRecord(_dt);
                }
            }
            return _fleetcardetail;
        }

        private FleetCarDetail FillDataRecord(DataTable myDataTable)
        {
            FleetCarDetail myCar = new FleetCarDetail();
            myCar.FleetCarId = Convert.ToInt64(myDataTable.Rows[0]["FleetCarId"].ToString());
            myCar.CompanyId = Convert.ToInt64(myDataTable.Rows[0]["CompanyId"].ToString());
            myCar.CarMakeName = myDataTable.Rows[0]["CarMake"].ToString();
            myCar.CarModelName = myDataTable.Rows[0]["CarModel"].ToString();
            myCar.Body = myDataTable.Rows[0]["Body"].ToString();
            myCar.CC = Convert.ToInt32(myDataTable.Rows[0]["CC"].ToString());
            myCar.Door = Convert.ToInt32(myDataTable.Rows[0]["Door"].ToString());
            myCar.GroupNo = Convert.ToInt32(myDataTable.Rows[0]["GroupNo"].ToString());
            myCar.MakeModelDesc = myDataTable.Rows[0]["MakeModelDesc"].ToString();
            myCar.ManufactYear = Convert.ToInt32(myDataTable.Rows[0]["ManufactYear"].ToString());
            myCar.AgentId = Convert.ToInt64(myDataTable.Rows[0]["AgentId"].ToString());
            myCar.RefCEUserId = Convert.ToInt64(myDataTable.Rows[0]["RefCEUserId"].ToString());
            myCar.Status = Convert.ToBoolean(myDataTable.Rows[0]["Status"].ToString());
            myCar.Info1 = myDataTable.Rows[0]["Info1"].ToString();
            myCar.Info2 = myDataTable.Rows[0]["Info2"].ToString();
            myCar.Info3 = myDataTable.Rows[0]["Info3"].ToString();
            myCar.Info5 = myDataTable.Rows[0]["Info5"].ToString();
            myCar.Info6 = myDataTable.Rows[0]["Info6"].ToString();
            myCar.Info7 = myDataTable.Rows[0]["Info7"].ToString();
            myCar.Info8 = myDataTable.Rows[0]["Info8"].ToString();
            myCar.Info9 = myDataTable.Rows[0]["Info9"].ToString();
            myCar.Info10 = myDataTable.Rows[0]["Info10"].ToString();
            myCar.Info11 = myDataTable.Rows[0]["Info11"].ToString();
            myCar.Info12 = myDataTable.Rows[0]["Info12"].ToString();
            myCar.Info13 = myDataTable.Rows[0]["Info13"].ToString();
            myCar.Info14 = myDataTable.Rows[0]["Info14"].ToString();
            myCar.Info15 = myDataTable.Rows[0]["Info15"].ToString();
            if (myDataTable.Rows[0]["BInfo1"].ToString() != "")
                myCar.BInfo1 = (bool)myDataTable.Rows[0]["BInfo1"];
            return myCar;
        }

    }
}